// import { getCurrentPages } from '@dcloudio/uni-app';
import { useConfigStore } from '@/store/modules/config'
const configStore = useConfigStore()
const backHomeTime = configStore.config?.backHomeTime
let redirectTimer = null; // 定义全局定时器

/**
 * 启动跳转到首页的逻辑
 * @param {number} timeout - 超时时间，默认 180000 毫秒
 */
export function startRedirectToHome(timeout = backHomeTime) {
  clearTimeout(redirectTimer);

  const currentPages = getCurrentPages();
  const currentPage = currentPages[currentPages.length - 1];
  if (!currentPage) return;

  const isHomePage = currentPage.route === 'pages/index'; // 判断是否是首页
  if (isHomePage) {
    return;
  }

  redirectTimer = setTimeout(() => {
    uni.reLaunch({ url: '/pages/index' }); // 跳转到首页
  }, timeout);
}

/**
 * 停止跳转到首页的逻辑
 */
export function stopRedirectToHome() {
  clearTimeout(redirectTimer);
  redirectTimer = null;
}
